﻿#ifndef JISTCOMMONMODEL_H
#define JISTCOMMONMODEL_H

#include <QAbstractListModel>
#include <QList>
#include <QRect>
#include <QTableWidgetItem>
//#include <QtGui>
#include <QGraphicsItem>
#include "ImageItem.h"
#include "../../libtiled/object.h"
#include <QString>

class ImageItem;
//class JistCommonModel;
class JistTableView;

struct JistCellData
{
    int m_iImgIndex;
    int m_iSpriteIndex;
    int m_Zlayer;
    float m_fAlpha;
    float m_fScale;
    float m_fScaleX;
    float m_fScaleY;
    short m_iRot;
    bool m_bFlip_H;
    bool m_bFlip_V;
    char m_iEff;
    char m_OrgX;
    char m_OrgY;
    int m_Px;
    int m_Py;
    float m_iDX;
    float m_iDY;
};

struct JistMapObjData
{
    int m_iImgIndex;
    int m_iSpriteIndex;
    int m_Px;
    int m_Py;

//    int m_Zlayer;
//    float m_fAlpha;
//    float m_fScale;
//    float m_fScaleX;
//    float m_fScaleY;
//    short m_iRot;
//    bool m_bFlip_H;
//    bool m_bFlip_V;
//    char m_iEff;

//    float m_iDX;
//    float m_iDY;
};



class JistCommonData// : public Tiled::Object
//struct JistCommonData
{
    public:
        JistCommonData()
        {
           m_pIItem = 0;
           m_UIOpt = 0;
           m_Name = QString();
           m_iSubCnt = 0;
           m_AnchorPoint = QPoint(0, 0);
        }
        void copyData(const JistCommonData& data);
        //seok 프로퍼티
        Tiled::Object m_Object;
        bool m_bVisible;
        QRect m_Rect;
        QRect m_CollisionRect;
        QString m_Name;
        QPoint m_Pos;
        QPoint m_AnchorPoint;
        QImage m_img;
        int m_Zlayer;
        ImageItem *m_pIItem;

        int m_iImgIndex;
        int m_iSpriteIndex;

    public:
        int m_UIOpt;
        int m_iSubCnt;


        /**
         * Returns the width of this tile.
         */
        int width() const { return m_img.width(); }

        /**
         * Returns the height of this tile.
         */
        int height() const { return m_img.height(); }

        void SetImgIndex(unsigned short index) { m_iImgIndex = index; }
        unsigned short GetImgIndex() { return m_iImgIndex; }

        void SetSpriteIndex(unsigned short index) { m_iSpriteIndex = index; }
        unsigned short GetSpriteIndex() { return m_iSpriteIndex; }
        const void SetName(QString name)   { m_Name = name; }

		// 스프라이트 추가시에만 적용
        bool operator ==(const JistCommonData &T) const {
             return (m_Rect == T.m_Rect && !m_pIItem);
        }
};


class JistCommonModel : public QAbstractListModel
{
    public:
        JistCommonModel(QObject *parent = NULL);
        void setViewWiget(JistTableView* view);
        int rowCount(const QModelIndex &parent = QModelIndex()) const;
        QVariant data(const QModelIndex &index, int role) const;
        bool setData(const QModelIndex &index, const QVariant &value, int role);
        bool insertRows(int position, int rows, const QModelIndex &index);
        bool removeRows(int position, int rows, const QModelIndex &index);
        void addData(JistCommonData data);
        void setImgFilePath(QString *imgFilePath);
        QString getImgFilePath();
        int toModelIndex(const QModelIndex &index) const;
        void setDataList(QList< JistCommonData > *list);
        void reset();

        int IndexToRow(int Index) const;
        void insertData(int index, JistCommonData *data);
        JistCommonData takeAt(int index);

    public:
        QList< JistCommonData > *m_pList;

        JistTableView *m_pView;
        QString m_ImgFilePath;
        int m_iRowCnt;

    signals:
        void add(int index);

};
#include "JistTableView.h"
#endif // JISTCOMMONMODEL_H
